<template>
  <view class="flex-col">
    <view :class="boole || type == 'ZD' ? 'bottom115' : 'marginBott'">
      <!--车辆信息 -->
      <carInformation v-model="ruleForm" title="车辆信息" :boole="boole" ref="carInformation"/>
      <orderInformation v-model="ruleForm" title="订单信息" :boole="boole" ref="orderInformation"/>
      <applyInformation v-model="ruleForm" title="申请信息" v-if="type !== 'ZD'" :boole="boole" ref="applyInformation"/>
      <resultInformation v-model="ruleForm" title="处置结果" v-if="!boole && type !== 'ZD'" :boole="boole"
                         ref="resultInformation"/>
      <!--订单信息-->
      <exam-flow v-if="(flowId && type !== 'ZD') || type == 'edit'" :instanceId="flowId"/>
    </view>

    <view class="button-container" v-if="boole || type == 'ZD'">
      <button class="btn-item" @click="submit">提 交</button>
    </view>
  </view>
</template>

<script>
import * as apis from '@/api/vehicleWarning'
import carInformation from './components/carInformation'
import orderInformation from './components/orderInformation'
import applyInformation from './components/applyInformation'
import resultInformation from './components/resultInformation'

export default {
  name: 'add',
  components: {carInformation, orderInformation, applyInformation, resultInformation},
  computed: {},
  watch: {},
  onLoad(op) {
    let title = ''
    this.flowId = op?.flowId
    this.type = op?.type
    if (op?.type === 'add') {
      title = '租购过户申请'
      this.boole = true
    }
    if (op?.type === 'edit') {
      title = '租购过户申请'
      this.boole = true
      this.handleCarIdData(op?.id)
    }
    if (op?.type === 'detail') {
      title = '租购过户详情'
      this.boole = false
      this.handleCarIdData(op?.id)
    }
    if (op?.type === 'ZD') {
      title = '租购过户申请'
      this.boole = false
      this.handleCarIdData(op?.id)
    }
    uni.setNavigationBarTitle({
      title,
    })
  },
  data() {
    return {
      ruleForm: {
        carId: '',
        carNo: '',
        vinNo: '',
        brandId: '',
        seriesId: '',
        modelId: '',
        brandName: '',
        modelName: '',
        seriesName: '',
        propertyRightId: '',
        propertyRightName: '',
        operationsName: '',
        operationsId: '',
        ascriptionId: '',
        ascription: '',
        outStorageTime: '',
        returnTime: '',
        financialPassedTime: '',
        orderNo: '',

        customerName: '',
        customerPhone: '',
        primaryContact: '',
        primaryContactPhone: '',
        primaryContactIdentity: '',
        secondaryContact: '',
        secondaryContactPhone: '',
        secondaryContactWorkDuties: '',
        schemeName: '',
        applyReason: '',
        applyAttachment: ''
      },
      boole: false,
      flowId: null,
      type: null,
    }
  },
  methods: {
    //carId   查询接口
    async handleCarIdData(id) {
      try {
        let res = await apis?.getTransferDetails(id)
        if (res.data) {
          this.ruleForm = {...res.data}
        }
      } catch (e) {
        console.error(e)
      }
    },
    async submit() {
      try {
        uni.showLoading()
        let vaildResult = await this._validate()
        if (Array.isArray(vaildResult)) {
          return;
        }
        if (this.type == 'add') {
          const res = await apis?.carLeasePurchaseTransfer(this.ruleForm)
          if (res.code === 200) {
            uni.$emit("refBackList", true);
            this.$modal.msgSuccess('提交成功')
            setTimeout(() => {
              this.$tab.navigateBack();
            }, 1000)
          }
        }
        if (this.type == 'ZD' || this.type == 'edit') {
          const res = await apis?.submitTransfer(this.ruleForm)
          if (res.code === 200) {
            uni.$emit("refBackList", true);
            this.$modal.msgSuccess('提交成功')
            setTimeout(() => {
              this.$tab.navigateBack();
            }, 1000)
          }
        }
      } catch (error) {
        this.$nextTick(() => {
          uni.pageScrollTo({
            selector: error,
          })
        })
      } finally {
        uni.hideLoading()
      }
    },
  },
}
</script>

<style scoped lang="scss">
.marginBott {
  margin-bottom: 20rpx !important;
}
</style>
